Ext.define("NF.view.farm.FarmTabs", {
    extend:'Ext.tab.Panel',
    alias:'widget.farmTabs',
    id:'farmTabsId',
    autoDestroy: true,
    layout:'fit',
    initComponent:function() {
        var self=this;
        var tabs=self;
        this.items = [
            {
                xtype:'panel',
                title: 'Home',
                frame:false,
                closable:false,
                id:'homeTab',
                itemId: 'home',
                tbar: [
                    { xtype: 'button', iconCls: 'bmenu', text:'Livestock Operations',menu: self.createMenuBar() },
                    { xtype: 'tbspacer',width: 500},
                    { xtype:'livestocksearchform'}
                ]
            }
        ];

        this.findTab = function(objectData){
            var tab=null;
            //We'll allow one 'generic' tab child and N with apolloOids
            var searchingForHash = objectData || {'generic': '' };
            searchingForHash = Ext.JSON.encode(searchingForHash);
            for(var i=0;i<tabs.items.length;i++){
                var item=self.items.items[i];
                var itemHash=Ext.JSON.encode(item.model);
                if(itemHash===searchingForHash){
                    tab=item;
                    break;
                }
            }
            return tab;
        };

        this.addOrActivateTab = function(objectData){
            var existingTab = self.findTab(objectData);
            if(!existingTab){
                existingTab = Ext.ComponentMgr.create({
                   xtype:objectData.xtype,
                   closable:true,
                   title:'Loading..',
                   model:objectData
                });
                tabs.add(existingTab);
            }
            tabs.setActiveTab(existingTab);
        }
        this.callParent(arguments);
    },
    createMenuBar:function(){
        var self=this;
        var menuBar = Ext.create('Ext.menu.Menu',{
            id:'createLiveStockMenuBar',
             defaults: {
                        xtype: 'button',
                        iconAlign: 'left'
                    },
            style: {
                overflow: 'visible'     // For the Combo popup
            },
            items: [
                     {
                        text: 'Create Livestock',
                        iconCls: 'add',
                        width: 'auto',
                        handler:function(){
                            var createLivestockForm = Ext.ComponentMgr.create({
                                xtype:'createLivestock',
                                closable:true,
                                title:'Loading..'
                            });
                            createLivestockForm.show();
                            return;
                        }
                    }
            ]
        });

        return menuBar;
    }/*,
    createSearchMenuBar:function(){
        var self=this;
        var menuBar = Ext.create('Ext.menu.Menu',{
            id:'searchLiveStockMenuBar',
             defaults: {
                        xtype: 'button',
                        iconAlign: 'left'
                    },
            style: {
                overflow: 'visible'     // For the Combo popup
            },
            items: [
                     {
                        text: 'Create Livestock',
                        iconCls: 'add',
                        width: 'auto',
                        handler:function(){
                            var createLivestockForm = Ext.ComponentMgr.create({
                                xtype:'createLivestock',
                                closable:true,
                                title:'Loading..'
                            });
                            createLivestockForm.show();
                            return;
                        }
                    }
            ]
        });

        return menuBar;
    }*/
});